package com.aliware.tianchi;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

/**
 * @author daofeng.xjf
 *
 * 服务端过滤器
 * 可选接口
 * 用户可以在服务端拦截请求和响应,捕获 rpc 调用时产生、服务端返回的已知异常。
 */
@Activate(group = Constants.PROVIDER)
public class TestServerFilter implements Filter {

    private ServerMsg serverMsg;
    public TestServerFilter(){
        serverMsg=new ServerMsg(System.getProperty("quota"));
    }

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        try{

            int total =  invoker.getUrl().getParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS);
            RpcStatus status = RpcStatus.getStatus(invoker.getUrl(), invocation.getMethodName());
            int active = status.getActive();
            int avail = total-active;
            serverMsg.setAvail(avail);

            Result result = invoker.invoke(invocation);
            return result;
        }catch (Exception e){
            throw e;
        }

    }

    @Override
    public Result onResponse(Result result, Invoker<?> invoker, Invocation invocation) {
        return result;
    }

}
